package com.zmj.framework.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmj.framework.base.service.IBaseService;
import com.zmj.framework.constant.CacheKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachePut;

/**
 * @author scy
 * 基础service 继承子plus提供的ServiceImpl
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService<T> {
    @Autowired
    public CacheManager cacheManager;

    public boolean getRepeatLock(String key) {
        Cache cache = cacheManager.getCache(CacheKey.NO_REPEAT_LOCK_PREFIX);
        if (cache == null) {
            return false;
        } else if (ObjectUtil.isNotNull(cache.get(key))) {
            return true;
        }
        return false;
    }

    @CachePut(value = CacheKey.NO_REPEAT_LOCK_PREFIX, key = "#key")
    public boolean setRepeatLock(String key) {
        return true;
    }
}
